package com.hcbd.tdwt.ui.xingyu.activity;

import static java.lang.Thread.sleep;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.hcbd.tdwt.MainApp;
import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.base.bd.RnssSerialPortManage;
import com.hcbd.tdwt.base.bd.XYSerialPortManage;
import com.hcbd.tdwt.base.bd.XyBdSendChild;
import com.hcbd.tdwt.base.bd.XyPdaBdSendChild;
import com.hcbd.tdwt.ui.xingyu.fragment.BdLocationUpFragment;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.lang.ref.WeakReference;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_pos_mode_setting)
public class PosModeSettingActivity extends BaseActivity {
    private String TAG = "PosModeSettingActivity";
    @ViewById
    TextView title;
    @ViewById
    Switch dataSources;
    @ViewById
    RadioGroup radio,antenna_group;
    @ViewById
    RadioButton gpsRb,bdRb,gpsBdRb,internal,external;
    RnssSerialPortManage rnssSerialPort;
    private MyHandler myHandler;
    private SharedPreferencesUtil sp;
    private final int bd_mode=1;
    private final int gps_mode=2;
    private final int bd_gps_mode=3;
    private final int antenna_i=0;
    private final int antenna_e=1;
    private final int source_system=-1;
    private final int source_model=1;
    private static class MyHandler extends Handler {
        //弱引用，在垃圾回收时，被回收
        WeakReference<PosModeSettingActivity> activityWeakReference;
        MyHandler(PosModeSettingActivity activity){
            this.activityWeakReference=new WeakReference<>(activity);
        }
        public void handleMessage(Message msg){
            activityWeakReference.get().hideLoading();
        }
    }
    @AfterViews
    protected void init() {
        title.setText("定位天线选择");
        rnssSerialPort=RnssSerialPortManage.getInstance();
        myHandler=new MyHandler(this);
        sp=new SharedPreferencesUtil(this);
        int source = sp.getAttributeInt(Constant.DATA_SOURCES);
        if(source==source_system){
            dataSources.setChecked(false);
        }else{
            dataSources.setChecked(true);
        }
        dataSources.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    sp.addAttribute(Constant.DATA_SOURCES,source_model);
//                    ((XyPdaBdSendChild) BdToolFactory.getBdSendBase()).openRN();  // 打开RN模块
//                    MainApp.getInstance().closeSystemLocation();  // 关闭系统定位监听
                }else{
//                    int pos_mode=sp.getAttributeInt(Constant.pos_mode);
//                    if(pos_mode==bd_mode){
//                        toast("单北斗定位模式下无法使用系统API获取数据");
//                        dataSources.setChecked(true);
//                        return;
//                    }
                    sp.addAttribute(Constant.DATA_SOURCES,source_system);
//                    ((XyPdaBdSendChild)BdToolFactory.getBdSendBase()).closeRN();  // 关闭RN模块
//                    MainApp.getInstance().initSystemLocation();  // 打开系统定位监听
                }
            }
        });

        init_position();
        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//                if(!BdToolFactory.getBdSendBase().enableRN){
//                    init_position();
//                    toast("请在数据来源为模块时进行操作");
//                    return;
//                }
                // 通过checkedId判断哪个RadioButton被选中了
                switch (checkedId) {
                    case R.id.gpsRb:
                        open_gps();
                        break;
                    case R.id.bdRb:
                        open_bd();
                        break;
                    case R.id.gpsBdRb:
                        open_bd_gps();
                        break;
                }
            }
        });

        init_antenna();
        antenna_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//                if(!BdToolFactory.getBdSendBase().enableRN){
//                    init_antenna();
//                    toast("请在数据来源为模块时进行操作");
//                    return;
//                }
                switch (checkedId) {
                    case R.id.internal:
                        set_antenna_i();
                        break;
                    case R.id.external:
                        set_antenna_e();
                        break;
                }
            }
        });
    }

    private void init_position(){
        int mode=sp.getAttributeInt(Constant.pos_mode);
        if(mode==bd_mode){
            bdRb.setChecked(true);
        }else if(gps_mode==mode){
            gpsRb.setChecked(true);
        }else{
            gpsBdRb.setChecked(true);
        }
    }

    private void init_antenna(){
        int antenna=sp.getAttributeInt(Constant.ANTENNA_SETTINGS);
        if(antenna==antenna_e){
            external.setChecked(true);
        }else{
            internal.setChecked(true);
        }
    }

    @Click
    void headLeft(){
        finish();
    }


    public void open_gps(){
        showLoading("加载中");
        new Thread(() -> {
            try{
                rnssSerialPort.sendPCas04(RnssSerialPortManage.TYPE_01);
                sleep(1000);
                rnssSerialPort.sendPCAS00();
                sleep(1000);
                rnssSerialPort.sendCFGSYS(RnssSerialPortManage.TYPE_H_01);
                sleep(2000);
                rnssSerialPort.sendCFGSAVE();
                myHandler.sendEmptyMessage(1);
                sp.addAttribute(Constant.pos_mode,gps_mode);
            }catch (Exception e){
                e.printStackTrace();
            }

        }).start();

    }
    public void open_bd(){
        showLoading("加载中");
        new Thread(() -> {
            try{
                rnssSerialPort.sendPCas04(RnssSerialPortManage.TYPE_12);
                sleep(1000);
                rnssSerialPort.sendPCAS00();
                sleep(1000);
                rnssSerialPort.sendCFGSYS(RnssSerialPortManage.TYPE_H_02);
                sleep(2000);
                rnssSerialPort.sendCFGSAVE();
                myHandler.sendEmptyMessage(1);
                sp.addAttribute(Constant.pos_mode,bd_mode);
            }catch (Exception e){
                e.printStackTrace();
            }

        }).start();

    }
    public void open_bd_gps(){
        showLoading("加载中");
        new Thread(() -> {
            try{
                rnssSerialPort.sendPCas04(RnssSerialPortManage.TYPE_13);
                sleep(1000);
                rnssSerialPort.sendPCAS00();
                sleep(1000);
                rnssSerialPort.sendCFGSYS(RnssSerialPortManage.TYPE_H_03);
                sleep(2000);
                rnssSerialPort.sendCFGSAVE();
                myHandler.sendEmptyMessage(1);
                sp.addAttribute(Constant.pos_mode,bd_gps_mode);
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void set_antenna_i(){
        showLoading("加载中");
        new Thread(() -> {
            try{
                XYSerialPortManage.getInstance().setBDPowerState(XYSerialPortManage.ANTENNA_I1);
//                rnssSerialPort.setAntenna(RnssSerialPortManage.ANTENNA_I1);
                sleep(1000);
                XYSerialPortManage.getInstance().setBDPowerState(XYSerialPortManage.ANTENNA_I2);
//                rnssSerialPort.setAntenna(RnssSerialPortManage.ANTENNA_I1);
                sleep(1000);
                myHandler.sendEmptyMessage(1);
                sp.addAttribute(Constant.ANTENNA_SETTINGS,antenna_i);
            }catch (Exception e){
                e.printStackTrace();
            }

        }).start();

    }
    public void set_antenna_e(){
        showLoading("加载中");
        new Thread(() -> {
            try{
                XYSerialPortManage.getInstance().setBDPowerState(XYSerialPortManage.ANTENNA_E1);
//                rnssSerialPort.setAntenna(RnssSerialPortManage.ANTENNA_E1);
                sleep(1000);
                XYSerialPortManage.getInstance().setBDPowerState(XYSerialPortManage.ANTENNA_E2);
//                rnssSerialPort.setAntenna(RnssSerialPortManage.ANTENNA_E2);
                sleep(1000);
                myHandler.sendEmptyMessage(1);
                sp.addAttribute(Constant.ANTENNA_SETTINGS,antenna_e);
            }catch (Exception e){
                e.printStackTrace();
            }

        }).start();

    }

}
